#include <fcntl.h>
#include <time.h>
#include <ctype.h>
#include <termio.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>

/* Error codes */
#define KDRV_SUCCESS         0
#define KDRV_TIMEOUT_ERROR   1001
#define KDRV_EXCEPTION_ERROR 1002
#define KDRV_CRC_ERROR       1003

/* Channel IDs */
#define KDRV_CHANNEL_CH0     0
#define KDRV_CHANNEL_P1      1
#define KDRV_CHANNEL_P2      2
#define KDRV_CHANNEL_T       3
#define KDRV_CHANNEL_TOB1    4
#define KDRV_CHANNEL_TOB2    5

/* Info */
#define KDRV_NAME "keller30drv"
#define KDRV_VERSION    "0.1"

#define FALSE 0
#define TRUE 1

/* Struct for storing response from Function 48 */
struct f48_response_s {
        char devaddr;
        char function_id;
        unsigned char dev_class;
        unsigned char dev_group;
        unsigned char year;
        unsigned char week;
        unsigned char buf_size;
        unsigned char stat;
        unsigned char crc16_H;
        unsigned char crc16_L;
};

/* Struct for storing response from Function 73 */
struct f73_response_s {
        char devaddr;
        char function_id;
        unsigned char byte3;
        unsigned char byte2;
        unsigned char byte1;
        unsigned char byte0;
        unsigned char stat;
        unsigned char crc16_H;
        unsigned char crc16_L;
};

/* Struct for storing response from Function 74 */
struct f74_response_s {
        char devaddr;
        char function_id;
        unsigned char byte3;
        unsigned char byte2;
        unsigned char byte1;
        unsigned char byte0;
        unsigned char stat;
        unsigned char crc16_H;
        unsigned char crc16_L;
};

/* Calculate CRC16 */
void CalcCRC16(unsigned char *SC_Buffer, int SC_Amount, unsigned char* CRC_H, unsigned char* CRC_L);

/* Function 48 : Initialise and releas */
long f48_initialise(int ttyfd, int slave, int timeout, struct f48_response_s *f48_response, int *error);

/* Function 73 :  Read value of a channel (floating point) */
float f73_read_channel(int ttyfd, int slave, int timeout, int channel, int *error);

/* Function 74 : Read value of a channel (integer) */
long f74_read_channel(int ttyfd, int slave, int timeout, int channel, int *error);

/* Returns an error string */
char *get_kdrv_error(int error);

/* Returns sensor channel ID */
int get_channel_id(char *channel);

/* Returns driver version */
char *get_driver_version();


